﻿
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using Microsoft.JSInterop;

namespace SageKing.Blazor.OpenIdService.Components.Base;

public class AComponentEditBase
    <T, K> : AComponentBase, IAddEdit<T, K>
   where T : IEntityBaseId<K>, new()
{
    #region Ref
    public ElementReference copyRef;
    public AntDesign.Input<string> inputFirst;
    #endregion

    #region init
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
    }
    #endregion

    #region Rules
    public FormValidationRule[] RequiredRules = new FormValidationRule[] {
        new FormValidationRule {  Required=true }
    };

    #endregion

    #region IAddEdit
    public virtual IAddEdit<T, K> Me => this;

    public virtual bool IsVersionValidation { get; set; } = false;

    public virtual IBaseService<T, K> DataService { get; set; }

    [Parameter]
    public virtual string Title { get; set; }

    [Parameter]
    public virtual T model { get; set; }

    public virtual Form<T> form { get; set; }


    [Parameter]
    public virtual Action<T, ChangedEnum> OnModelChanged { get; set; }

    #endregion

    #region Form
    public virtual void OnFinish(EditContext editContext)
    {
        Console.WriteLine($"Success:{model.toJsonStr()}");
    }

    public virtual void OnFinishFailed(EditContext editContext)
    {
        Console.WriteLine($"Failed:{model.toJsonStr()}");
    }
    #endregion

    #region UI
    public async Task FocusFirst()
    {
        await inputFirst.Focus();
    }
    #endregion

}
